package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.jianyun.jyzs.bean.AnnouncementList;
import com.jianyun.jyzs.db.DatabaseHelper;
import com.jianyun.jyzs.utils.LoginCache;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/11/20.
 */

public class AnnouncementDao {
    private Context context;
    private Dao<AnnouncementList.Announcement, Integer> dao;
    private DatabaseHelper helper;
    private final String userId;

    public AnnouncementDao(Context context) {
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            dao = helper.getDao(AnnouncementList.Announcement.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insert(AnnouncementList.Announcement aa, String enterpriseCode) {
        try {

            AnnouncementList.Announcement announment = getAnnounment(aa.getNewsId(), enterpriseCode);
            if (announment != null) {
                aa.setIsdel(announment.isdel());
                aa.setEnterpriseCode(enterpriseCode);
                aa.setIsread(announment.isread());
                aa.setUserid(userId);
                dao.createOrUpdate(aa);
            } else {
                aa.setEnterpriseCode(enterpriseCode);
                aa.setUserid(userId);
                dao.createOrUpdate(aa);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public AnnouncementList.Announcement getAnnounment(String newsid, String enterpriseCode) {
        List<AnnouncementList.Announcement> list = new ArrayList<>();
        QueryBuilder<AnnouncementList.Announcement, Integer> builder = dao.queryBuilder();
        try {
            list = builder.where().eq("newsId", newsid).and().eq("enterpriseCode", enterpriseCode).and().eq("userid",userId).query();
            if (list.size() > 0) {
                return list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public List<AnnouncementList.Announcement> getAnnounmentList(String enterpriseCode) {
        List<AnnouncementList.Announcement> list = new ArrayList<>();
        QueryBuilder<AnnouncementList.Announcement, Integer> builder = dao.queryBuilder().orderBy("intId", false);
        try {
            builder.where().eq("enterpriseCode", enterpriseCode).and().ne("isdel", true).and().eq("userid",userId);
            list = builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void del(String newsid, String enterpriseCode) {

        AnnouncementList.Announcement announment = getAnnounment(newsid, enterpriseCode);
        if (announment != null) {
            announment.setIsdel(true);
            try {
                dao.createOrUpdate(announment);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void update(AnnouncementList.Announcement bean) {
        RceLog.d("lml", "更新已读状态" + bean.toString());
        try {
            bean.setUserid(userId);
            dao.createOrUpdate(bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void coninsert(AnnouncementList.Announcement response) {
        String enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        try {
            response.setEnterpriseCode(enterpriseCode);
            response.setUserid(userId);
            dao.createOrUpdate(response);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
